<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.png">
  <link rel="mask-icon" href="/images/avatar.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"notes.maxwi.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="1.面向对象编程的核心思想是数据抽象、继承和动态绑定。   派生类2.C++中，基类将类型相关的函数（即派生类可能需要改变的函数）与派生类不做改变直接继承的函数区分对待。对于基类希望它的派生类各自定义适合自身版本的函数，基类需要将这些函数声明为虚函数（virtual function）。">
<meta property="og:type" content="article">
<meta property="og:title" content="C++学习笔记之面向对象程序设计">
<meta property="og:url" content="http://notes.maxwi.com/2016/01/13/cpp-oop-object-oriented/index.html">
<meta property="og:site_name" content="blueyi&#39;s notes">
<meta property="og:description" content="1.面向对象编程的核心思想是数据抽象、继承和动态绑定。   派生类2.C++中，基类将类型相关的函数（即派生类可能需要改变的函数）与派生类不做改变直接继承的函数区分对待。对于基类希望它的派生类各自定义适合自身版本的函数，基类需要将这些函数声明为虚函数（virtual function）。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2016-01-13T08:21:39.000Z">
<meta property="article:modified_time" content="2016-01-13T08:21:39.000Z">
<meta property="article:author" content="blueyi">
<meta property="article:tag" content="Notes">
<meta property="article:tag" content="CPP-OOP">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://notes.maxwi.com/2016/01/13/cpp-oop-object-oriented/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>C++学习笔记之面向对象程序设计 | blueyi's notes</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="blueyi's notes" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="blueyi's notes" type="application/rss+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">blueyi's notes</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Follow Excellence,Success will chase you!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://notes.maxwi.com/2016/01/13/cpp-oop-object-oriented/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/default_avatar.jpg">
      <meta itemprop="name" content="blueyi">
      <meta itemprop="description" content="心怀善意，虛怀若谷！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="blueyi's notes">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          C++学习笔记之面向对象程序设计
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2016-01-13 16:21:39" itemprop="dateCreated datePublished" datetime="2016-01-13T16:21:39+08:00">2016-01-13</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C++</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="Symbols count in article">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">Symbols count in article: </span>
              <span>13k</span>
            </span>
            <span class="post-meta-item" title="Reading time">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">Reading time &asymp;</span>
              <span>12 mins.</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>1.面向对象编程的核心思想是数据抽象、继承和动态绑定。  </p>
<h2 id="派生类"><a href="#派生类" class="headerlink" title="派生类"></a>派生类</h2><p>2.C++中，基类将类型相关的函数（即派生类可能需要改变的函数）与派生类不做改变直接继承的函数区分对待。对于基类希望它的派生类各自定义适合自身版本的函数，基类需要将这些函数声明为虚函数（virtual function）。  </p>
<span id="more"></span>
<p>3.派生类必须通过使用类派生列表明确指定它从哪个基础派生而来，类派生列表即类名后面跟冒号，然后是基类列表，每个基类前面可以有访问说明符。派生类必须在其内部对所有重新定义的虚函数进行声明。声明方式可以在函数前加关键字virtual，或者使用C++11中的在后面添加override。但派生类不需要桥头所有继承而来的虚函数，如果派生类没有覆盖其基类中的某个虚函数，则该虚函数的行为类似其他普通成员，派生类会直接继承其在基类中的版本。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Quote</span> &#123;</span><br><span class="line"><span class="keyword">public</span>: </span><br><span class="line">  <span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line">  <span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span> n)</span> <span class="type">const</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Quote &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span> n)</span> <span class="type">const</span> <span class="keyword">override</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>4.使用基类的引用（或指针）调用一个虚函数时将发生运行时动态绑定。即当某个函数形参是基类的引用时，可以为其传递一个基类的引用，也可以为其传递一个派生类的引用，在该函数内部调用虚函数时，将在运行时根据传递的是基类还是派生类来决定调用的是基类的虚函数还是派生类的虚函数。  </p>
<p>5.基类通常都应该定义一个虚析构函数，即使该函数不执行任何实际操作。  </p>
<p>6.任何构造函数之外的非静态函数都可以是虚函数，关键字virtual只能出现在类内部的声明语句之前而不能用于类外部的函数定义。如果基类把一个函数声明为虚函数，则该函数在派生类中也将隐式地成为虚函数。当使用指针或引用调用虚函数时，该调用将被动态绑定。成员函数如果没被声明为虚函数，则其解析过程将发生在编译时而非运行时。  </p>
<p>9.编译器可以隐式地执行派生类到基类的转换，所以之所有可以将基类的指针或引用绑定到了派生类，实际上是发生了动态转换。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Quote item;             <span class="comment">//基类</span></span><br><span class="line">Bulk_quote bulk;        <span class="comment">//派生类</span></span><br><span class="line">Quote *p = &amp;item;       <span class="comment">//p指向Quote对象</span></span><br><span class="line">p = &amp;bulk;              <span class="comment">//p指向bulk的Quote部分</span></span><br><span class="line">Quote &amp;r = bulk;        <span class="comment">//r绑定到bulk的Quote部分</span></span><br></pre></td></tr></table></figure>

<p>10.每个类控制它自己的成员初始化。尽管派生类对象中含有从基类继承而来的成员，但派生类并不能直接初始化这些成员。派生类必须使用基类的构造函数来初始化它的基类部分。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Bulk_quote构造函数</span></span><br><span class="line"><span class="built_in">Bulk_quote</span>(<span class="type">const</span> std::string&amp; book, <span class="type">double</span> p, std::<span class="type">size_t</span> qty, <span class="type">double</span> disc) : <span class="built_in">Quote</span>(book, p), <span class="built_in">min_qty</span>(qty), <span class="built_in">discount</span>(disc) &#123;&#125;</span><br></pre></td></tr></table></figure>
<p>初始化过程：首先初始化基类的部分，然后按照声明的顺序依次初始化派生类的成员。  </p>
<p>11.如果基类定义了一个静态成员，则整个继承体系中只存在该成员的唯一定义。不论从基类派生出多少个派生类，对于每个静态成员来说都只存在唯一实例。也就是说调用时派生类和派生类对象可以直接调用基类中定义的静态成员。  </p>
<p>12.派生类的声明不包含派生列表（class Bulk_quote;）。但被用作基类的类必须定义而非仅仅声明。  </p>
<p>13.派生类也可以被继承。如果D为基类，D1继承自D，D2继承自D1，则D是D1的直接基类（direct base），同时是D2的间接基类（indirect base）。但类不能继承自它自己  </p>
<p>14.C++11中可以在类名后面跟关键字final来指定某个类不能被继承。  </p>
<p>15.派生类向基类的类型转换中存在静态类型与动态类型的问题。静态类型（static type）是指在编译时就已知其类型，它是变量声明时的类型或表达式生成的类型，如果表达式即不是指针也不是引用，则它的动态类型与静态类型永远一致。动态类型（dynamic type）是指变量或表达式在内存中的对象的类型，动态类型只到运行时才可知。例如派生类的对象可以赋给基类的指针或引用，此时基类的指针或引用在声明时便是静态类型，但在运行时却是动态类型，因为此时该指针或引用即可以接受一个派生类对象，也可以接受一个基类对象。所以只有基类的指针或引用的静态类与动态类型才有可能不一致。  </p>
<p>16.不存在从基类向派生类的隐式类型转换，因为每个派生类对象中都包含一个基类部分，而基类对象中并不一定包含派生类的部分，当将一个基类的指针或引用绑定到派生类时，实际上是绑定到该派生类对象的基类部分上。派生类向基类的自动类型转换只针对指针或引用 有效。  </p>
<h2 id="虚函数"><a href="#虚函数" class="headerlink" title="虚函数"></a>虚函数</h2><p>通过虚函数可以实现通过基类的引用来访问派生类中通过基类继承而来的虚函数。其中一个用处就是可以定义一个基类的变量，这个变量即可以指向基类对象，又可以指向派生类对象，那么通过该变量也就即可以访问基类成员又可以访问派生类的成员。<br>1.必须为每一个虚函数都提供定义，因为基类的引用或指针调用一个虚成员函数时会执行动态绑定，直到运行时才能知道到底调用了哪个版本的虚函数，因为在运行时用户可以根据需要来确定给虚函数提供的参数是基类的对象还是派生类的对象。  </p>
<p>2.OOP的核心思想是多态性(polymorphism)。其含义是多种形式，具有继承关系的多个类型称为多态性，因为我们能使用这些类型的“多种形式”而无须在意它们之间的差异。例如基类的引用或指针可以动态绑定到派生类，从而通过虚函数来执行派生类的相应函数，在该情况下该引用或指针所绑定的对象可能是一个基类的对象也可能是一个派生类的对象，相应的执行的虚函数也就有可能是基类的版本，也有可能是派生类的版本。引用或指针的静态类型与动态类型不同这一事实是C++支持多态性的根本所在。  </p>
<p>3.一个函数一旦被声明成了虚函数，则在有派生类中它都是虚函数。如果在派生类的覆盖了某个继承而来的虚函数，而它的形参类型和函数返回值必须与被它覆盖的基类函数完全一致。返回值的一个例外是当类的虚函数返回类型时类本身的指针或引用时，此时基类返回的类型肯定是基类的，派生类自然也可以返回派生类自己的。注意此处与函数重载是完全不一样的，重载只发生在同一个作用域之内。当然派生类中可以声明与基类形参列表不同但同名的函数，但此时该函数将与基类函数是相互独立的，且编译器不会报错，解决这个问题需要使用override关键字。  </p>
<p>4.C++11中可以在派生类的虚函数中使用override关键字用来表明该函数将会覆盖基类中的虚函数，如果此时形参列表不致，编译将报错。<br>举例：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Base</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Base</span>(<span class="type">const</span> Base&amp;) = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Base</span>(<span class="type">int</span> i) : <span class="built_in">x</span>(i) &#123;&#125;;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">int</span> <span class="title">return_val</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> x; &#125;; <span class="comment">//虚函数</span></span><br><span class="line">    ~<span class="built_in">Base</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">protected</span>:  <span class="comment">//为了使派生类可以访问该成员，需要声明为受保护的</span></span><br><span class="line">    <span class="type">int</span> x;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Derive</span> : <span class="keyword">public</span> Base &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Derive</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Derive</span>(<span class="type">const</span> Derive&amp;) = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Derive</span>(<span class="type">int</span> i) : <span class="built_in">Base</span>(i), <span class="built_in">y</span>(i) &#123;&#125;;</span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">return_val</span><span class="params">()</span> <span class="type">const</span> <span class="keyword">override</span> </span>&#123; <span class="keyword">return</span> x * y; &#125;; </span><br><span class="line">    ~<span class="built_in">Derive</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> y; </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//形参item将即可接受Base对象，又可以接受其派生类对象</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">print_val</span><span class="params">(<span class="type">const</span> Base &amp;item, std::ostream &amp;os = std::cout)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    os &lt;&lt; item.<span class="built_in">return_val</span>() &lt;&lt; std::endl;  <span class="comment">//return_val将根据item所指向的不同对象调用不同类的虚函数</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Base <span class="title">b1</span><span class="params">(<span class="number">5</span>)</span></span>;</span><br><span class="line">    <span class="function">Derive <span class="title">d1</span><span class="params">(<span class="number">6</span>)</span></span>;</span><br><span class="line">    <span class="comment">//使用同一个函数print_val分别输出b1和d1的值</span></span><br><span class="line">    <span class="built_in">print_val</span>(b1);</span><br><span class="line">    <span class="built_in">print_val</span>(d1);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>5.当把某个函数声明为final时，则之后任何尝试覆盖该函数的操作都将引发编译错误。final和override说明符出现在形参列表（包括任何const或引用修饰符）以及尾置返回类型之后。  </p>
<p>6.如果在虚函数中使用默认实参，则通过基类的引用或指针调用函数时，不管派生类中定义的默认实参值是什么，都将使用基类的默认实参。所以基类和派生类的默认最好一致。  </p>
<p>7.可以通过使用作用域运算符来强制回避虚函数的动态绑定。如：  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//强行调用基类中定义的函数版本而不管baseP的动态类型是什么</span></span><br><span class="line"><span class="type">double</span> undiscounted = baseP-&gt;Quote::<span class="built_in">net_price</span>(<span class="number">42</span>);</span><br></pre></td></tr></table></figure>

<p>8.如果一个派生类的虚函数需要调用它的基类版本，而没有使用作用域运算符明确指定，则在动行时该调用将被解析为对派生类版本自身的调用 ，从而导致无限递归。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">A</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">print</span><span class="params">(ostream &amp;os)</span> <span class="type">const</span>  </span>&#123; os &lt;&lt; <span class="string">&quot;hi&quot;</span>; &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> : <span class="keyword">public</span> A &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="comment">//下面的定义将导致无限递归，因为默认调用的将是B自己的print函数</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">print</span><span class="params">(ostream &amp;os)</span> <span class="type">const</span> <span class="keyword">override</span> </span>&#123;<span class="built_in">print</span>(os); os &lt;&lt; <span class="string">&quot;world&quot;</span>; &#125;</span><br><span class="line">  <span class="comment">//修改为</span></span><br><span class="line">  <span class="comment">//void print(ostream &amp;os) const override &#123; A::print(os); os &lt;&lt; &quot;world&quot;; &#125;</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>9.关于虚函数表/虚表(vtable)，当类中包含有虚成员函数时，在用该类实例化对象时，对象的第一个成员将是一个指向虚函数表的指针，gdb打印显示为_vptr。该虚函数表记录运行过程中实际应该调用的所有虚函数的入口地址。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//抽象基类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">b</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">b</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">b</span>(<span class="type">int</span> i) : <span class="built_in">x</span>(i) &#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">print</span><span class="params">()</span> <span class="type">const</span> </span>= <span class="number">0</span>;  <span class="comment">//纯虚函数</span></span><br><span class="line">    <span class="keyword">virtual</span> ~<span class="built_in">b</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">    <span class="type">int</span> x;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//派生类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">d</span> : <span class="keyword">public</span> b &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">d</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">d</span>(<span class="type">int</span> i, <span class="type">int</span> j) : <span class="built_in">b</span>(i), <span class="built_in">y</span>(j) &#123;&#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">print</span><span class="params">()</span> <span class="type">const</span> <span class="keyword">override</span> </span>&#123;std::cout &lt;&lt; x &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; y &lt;&lt; std::endl;&#125;  <span class="comment">//重写纯虚函数</span></span><br><span class="line">    <span class="keyword">virtual</span> ~<span class="built_in">d</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">    <span class="type">int</span> y;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//普通类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">a</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">a</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">a</span>(<span class="type">int</span> i) : <span class="built_in">x</span>(i) &#123;&#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">print</span><span class="params">()</span> <span class="type">const</span> </span>&#123;std::cout &lt;&lt; x &lt;&lt; std::endl;&#125;</span><br><span class="line">    ~<span class="built_in">a</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">    <span class="type">int</span> x;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">d <span class="title">dd</span><span class="params">(<span class="number">1</span>, <span class="number">2</span>)</span></span>;     <span class="comment">//含有虚函数的派生类的对象</span></span><br><span class="line">    dd.<span class="built_in">print</span>();</span><br><span class="line">    <span class="function">a <span class="title">aa</span><span class="params">(<span class="number">1</span>)</span></span>;        <span class="comment">//普通类的对象</span></span><br><span class="line">    aa.<span class="built_in">print</span>();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用gdb调试时，分别打印dd和aa，发现dd的第一个成员是一个指向虚表的指针。  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) info locals </span><br><span class="line">dd = &#123;&lt;b&gt; = &#123;_vptr.b = <span class="number">0x400e30</span> &lt;vtable <span class="keyword">for</span> d+<span class="number">16</span>&gt;, x = <span class="number">1</span>&#125;, y = <span class="number">2</span>&#125;</span><br><span class="line">aa = &#123;x = <span class="number">1</span>&#125;</span><br></pre></td></tr></table></figure>

<p>10.虚析构函数的意义主要是为了防止内存泄漏。因为当一个基类的指针指向派生类的对象时，如果基类的析构函数不是虚函数，则通过该指针来delete派生类对象，系统将只执行基类的析构函数。因为只有通过虚函数实现的多态才能使用基类的指针访问派生类的成员。所以为了避免由于这种情况引起的无法正常析构派生类，往往将析构函数声明为虚函数，这样，执行delete时，系统将先执行派生类对象的析构函数，再执行基类的析构函数。  </p>
<h2 id="抽象基类-纯虚函数"><a href="#抽象基类-纯虚函数" class="headerlink" title="抽象基类-纯虚函数"></a>抽象基类-纯虚函数</h2><p>可以通过将类定义成抽象基类来禁止用户创建该类的对象，通过在函数内部定义纯虚函数即可将一个类定义成抽象基类，无法创建抽象基类的对象。<br>1.纯虚函数（pure virtual）无须定义，只需要在函数体的位置（即声明语句的分号之前）添加<code>=0</code>即可将一个虚函数声明为纯虚函数。<code>=0</code>只能出现的类内部的虚函数声明语句处，当然这个虚函数可以是从基类继承而来的，此时可以不写函数声明前面的virtual关键字。如果在基类中定义纯虚函数，则必须带上virtual关键字。纯虚函数也可以有定义，但定义必须在类的外部，即不能在函数内部为一个=0的函数提供函数体。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">virtual</span> <span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span> n)</span> <span class="type">const</span> </span>= <span class="number">0</span>;</span><br></pre></td></tr></table></figure>

<p>2.含有纯虚函数的类是抽象基类（abstract base class），抽象基类负责定义接口，后续的其他类可以覆盖该接口提供具体实现。如果一个派生自抽象基类的派生类不给纯虚函数提供定义，则该派生类仍将是抽象基类。  </p>
<p>3.派生类的构造函数只初始化它的直接基类，即不管继承多少次，派生类的构造函数都只负责初始化它自己新定义的成员，基类的成员将由基类的构造函数来初始化。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">A</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="built_in">A</span>() = <span class="keyword">default</span>;</span><br><span class="line">  <span class="built_in">A</span>(<span class="type">int</span> x) : <span class="built_in">xa</span>(x) &#123;&#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">  <span class="type">int</span> xa;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//派生类B将参数x传递给A的构造函数，由A的构造函数来初始xa，它只负责初始化自己新增加的xb</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> : <span class="keyword">public</span> A &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="built_in">B</span>() = <span class="keyword">default</span>;</span><br><span class="line">  <span class="built_in">B</span>(<span class="type">int</span> x, <span class="type">int</span> y) : <span class="built_in">A</span>(x), <span class="built_in">xb</span>(y) &#123;&#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">  <span class="type">int</span> xb;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>4.抽象类的指针可以指向它的所有派生类对象，并调用派生类中的虚函数。  </p>
<h2 id="访问控制"><a href="#访问控制" class="headerlink" title="访问控制"></a>访问控制</h2><ul>
<li>public成员：      可以被该类中的函数、友元函数、子类中的函数、类的对象访问  </li>
<li>protected成员：   可以被该类中的函数、友元函数、子类中的函数访问  </li>
<li>private成员：     可以被该类中的函数、友元函数访问</li>
</ul>
<p>protected成员对于类的用户来说与私有成员一样，无法被类的用户访问，但对于派生类的成员和友元来说是可访问的。但派生类的成员或友元只能通过派生类对象来访问基类的protected成员，而派生类对于基类对象中的受保护成员没有任何访问权限，也就是说派生类的成员和友元只能访问派生类对象中的基类部分的受保护成员，因为派生类是继承而类的受保护成员。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> &#123;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">  <span class="type">int</span> prot;  <span class="comment">//protected成员</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dri</span> : <span class="keyword">public</span> B &#123;</span><br><span class="line">  <span class="function"><span class="keyword">friend</span> <span class="type">void</span> <span class="title">clob</span><span class="params">(Dri&amp;)</span></span>;  <span class="comment">//能访问Dri::prot</span></span><br><span class="line">  <span class="function"><span class="keyword">friend</span> <span class="type">void</span> <span class="title">clob</span><span class="params">(B&amp;)</span></span>;    <span class="comment">//不能访问B::prot</span></span><br><span class="line">  <span class="type">int</span> j;  <span class="comment">//默认是private</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//正确：clob可以访问Dri对象的private和protected成员</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">clob</span><span class="params">(Dri &amp;d)</span> </span>&#123;d.j = d.prot = <span class="number">0</span>;&#125;</span><br><span class="line"><span class="comment">//错误：clob不能访问基类的protected成员  </span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">clob</span><span class="params">(B &amp;b)</span> </span>&#123;b.prot = <span class="number">0</span>;&#125;</span><br></pre></td></tr></table></figure>

<p><strong>注意：访问权限是针对类外部对类对象数据的访问权限</strong><br>如：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Bass</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Bass</span>(<span class="type">int</span> num = <span class="number">0</span>) : <span class="built_in">a</span>(num) &#123;&#125;</span><br><span class="line">    <span class="built_in">Bass</span>(<span class="type">const</span> Bass &amp;b);</span><br><span class="line">    ~<span class="built_in">Bass</span>() = <span class="keyword">default</span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> a;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Bass::<span class="built_in">Bass</span>(<span class="type">const</span> Bass &amp;b)</span><br><span class="line">&#123;</span><br><span class="line">    a = b.a;   <span class="comment">//这里b可以直接引用其私有变量a，因为这是在类内部，a也是同一个类的变量</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">   <span class="function">Bass <span class="title">b</span><span class="params">(<span class="number">5</span>)</span></span>;</span><br><span class="line">   std::cout &lt;&lt; b.a;   <span class="comment">//错误，a是b的私有变量，cout无法访问其私有变量</span></span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="继承权限"><a href="#继承权限" class="headerlink" title="继承权限"></a>继承权限</h2><p>1.某个类对于它继承而来的成员的访问权限受两个因素影响：一是基类中该成员的访问说明符，二是派生类的派生列表中的访问说明符<br>SOF上的解答：<a target="_blank" rel="noopener" href="http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance">Difference between private, public, and protected inheritance</a><br>继承权限主要影响派生类中继承自基类的成员的被访问权限（即这些继承而来的成员是对否派生类的用户可见），而派生访问说明符对于派生类的成员（及友元）能否访问其直接基类的成员没有影响，对基类成员的访问权限只与基类中的访问说明符有关。根据基类的访问说明符可以有public、protected、private三种情况，子类中相应的权限有：  </p>
<ul>
<li>public： 基类中的public、protected成员在子类中依然不变，当然private成员子类没有权限访问（这也是不指定继承权限时的默认情况）  </li>
<li>protected：基类中的public、protected成员在子类中都将变成protected。</li>
<li>private： 基类中的三种访问权限的成员在子类中都将变成private。  </li>
</ul>
<p>2.如果派生是公有的，则基类的公有成员也是派生接口的组成部分，这样就可以将公有派生类型的对象绑定到基类的引用或指针上，即实现动态绑定。<br>也就是说只有当派生是公有的时候，对于用户代码来说才能使用派生类向基类的类型转换。如果派生方式是私有的或者是受保护的，则用户代码不能使用该转换。  </p>
<p>3.不论派生类以什么方式继承自基类，派生类的成员函数和友元都能使用派生类向基类的转换。  </p>
<p>4.类的设计建议：基类应该将其接口成员声明为公有的，同时将属于其实现的部分分成两组：一组可供派生类访问，即将该组声明为受保护的，这样派生类就能在实现自己的功能时使用基类的这些受保护的操作和数据；另一组只能由基类及基类的友元访问，即将该组声明为私有的。  </p>
<p>5.友元与继承权限：<br>友元关系不能传递，同样友元关系也不能继承，基类的友元在访问派生类的成员时不具有特殊性，类似的，派生类的友元也不能随意访问基类的成员。但基类友的元可以访问内嵌在其派生类对象中的情况（因为每个派生类都包含一个完整的基类，那么基类的友元就可以通过派生类的对象来访问基类的成员，当然该基类成员也是派生类的成员）。</p>
<p>6.可以使用using指令来修改派生类继承的个别名字的访问级别。通过在类的内部使用using声明语句，可以将该类的直接或间接基类中的任何可访问成员（例如，非私有成员）标记出来，从而改变派生类中该基类成员的被访问权限。using声明语句中名字的访问权限由该using声明语句之前的访问说明符来决定。即，如果一条using声明语句出现在类的private部分，则该名字只能被类的成员和友元访问；相应的可以将其放在public和protected部分从而改变其被访问权限。<br>派生类只能为那些它可以访问的名字提供using声明。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> &#123;</span><br><span class="line"><span class="keyword">public</span>: </span><br><span class="line">  <span class="function"><span class="type">int</span> <span class="title">size</span><span class="params">()</span> <span class="type">const</span> </span>&#123;<span class="keyword">return</span> n;&#125;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">  <span class="type">int</span> n;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">D</span> : <span class="keyword">private</span> B &#123;  <span class="comment">//注意private继承</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="keyword">using</span> B:size;</span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">  <span class="keyword">using</span> B::n;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>7.默认的继承保护级别由定义派生类所用的关键字来决定。默认情况下使用class关键字定义的派生类当省略继承权限控制关键字时是私有继承，而使用struct关键字定义的派生类是公有继承。eg: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> &#123;<span class="comment">/*...*/</span> &#125;;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">D1</span> : B &#123;<span class="comment">/*...*/</span> &#125;;  <span class="comment">//默认public继承</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">D2</span> : B &#123;<span class="comment">/*...*/</span> &#125;;  <span class="comment">//默认private继承</span></span><br></pre></td></tr></table></figure>
<p>使用struct还是class定义类之间的唯一差别就是默认成员访问说明符及默认派生访问说明符。   </p>
<h2 id="继承中的类作用域"><a href="#继承中的类作用域" class="headerlink" title="继承中的类作用域"></a>继承中的类作用域</h2><p>1.当存在继承关系时，派生类的作用域嵌套到其基类的作用域之内。所以如果一个名字在派生类中无法正确解析，编译器将在外层的基类作用域内查找。<br>2.一个对象、引用或指针的静态类型决定了该对象的哪些成员是可见的，即使静态类型与动态类型可能不一致。当发生动态绑定时，由于形参使用的是基类的引用或指针，在使用该引用或指针的函数内部调用虚函数时将发生动态绑定，也就是在运行时才会解析该调用是使用派生类的虚函数还是基类的虚函数（注意此时派生类中的虚函数是继承自基类的）。但如果将一个基类的指针绑定到一个派生类，然后直接使用该指针调用派生类中新定义的成员将是非法的，因为此时实际上是产生了派生类向基类的隐式类型转换，所以在该指针所指的对象中只有基类的成员，并不存在派生类的成员。</p>
<p>3.派生类的成员将隐藏同名的基类成员，所以除了覆盖继承而来的虚函数之外，派生类最好不要重用其他定义在基类中的名字。</p>
<p>4.可以使用域运算符<code>::</code>通过基类来调用被隐藏的虚函数。  </p>
<p>5.成员函数无论是否是虚函数都能被重载，派生类可以覆盖重载函数的0个或多个实例，如果派生类希望基类中所有的重载版本对于它来说都是可见的，那么它就需要覆盖所有的版本，或者一个也不覆盖。由于<code>using</code>声明语句可以指定一个名字，所以可以使用一条基类成员函数的using声明语句来把该函数的所有重载实例添加到派生类作用域中，此时派生类只需要定义其特有的函数就可以了。注意using声明作用的成员必须是派生类可访问的。  </p>
<h2 id="继承体系中的拷贝控制"><a href="#继承体系中的拷贝控制" class="headerlink" title="继承体系中的拷贝控制"></a>继承体系中的拷贝控制</h2><p>1.如果基类的析构函数不是虚函数，则delete一个指向派生类对象的基类指针将产生未定义的行为，所以为了能够动态分配继承体系中的对象需要把函数声明为虚函数。虚函数将阻止合成默认的移动操作，即使通过=default的形式使用了合成的版本，编译器也不会为这个类合成移动操作。  </p>
<p>2.在默认情况下，基类默认构造函数初始化派生类对象的基类部分，如果我们想拷贝或移动基类部分，则必须在派生类的构造函数初始值列表中显式地使用基类的拷贝或移动构造函数。同样，派生类的赋值运算符也必须显式地为其基类部分赋值。但派生类的析构函数只负责销毁由派生类自己分配 的资源，对象的基类部分将由基类的析构函数隐式销毁。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> &#123;<span class="comment">/*...*/</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">D</span> : <span class="keyword">public</span> B &#123;</span><br><span class="line"><span class="keyword">public</span>::</span><br><span class="line">    <span class="comment">//默认情况下，基类的默认构造函数初始化对象的基类部分</span></span><br><span class="line">    <span class="comment">//要想使用拷贝或移动构造函数，我们必须在构造函数初始化列表中显式地调用该构造函数</span></span><br><span class="line">    <span class="built_in">D</span>(<span class="type">const</span> D&amp; d) : <span class="built_in">B</span>(d)   <span class="comment">//拷贝基类成员</span></span><br><span class="line">                        <span class="comment">/*D的成员初始值*/</span> &#123;<span class="comment">/*...*/</span>&#125;</span><br><span class="line">    <span class="built_in">D</span>(D&amp;&amp; d) : <span class="built_in">B</span>(std::<span class="built_in">move</span>(d))   <span class="comment">//移动基类成员</span></span><br><span class="line">                        <span class="comment">/*D的成员初始值*/</span> &#123;<span class="comment">/*...*/</span>&#125;</span><br><span class="line">    D&amp; <span class="keyword">operator</span>=(<span class="type">const</span> D &amp;rhs) </span><br><span class="line">    &#123;</span><br><span class="line">      B::<span class="keyword">operator</span>=(rhs);  <span class="comment">//为基类部分赋值</span></span><br><span class="line">      <span class="comment">//按照过去的方式为派生类的成员赋值</span></span><br><span class="line">      <span class="comment">//记得处理自赋值及释放已有资源等情况</span></span><br><span class="line">      <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//B::~B()被隐式自动调用</span></span><br><span class="line">    ~<span class="built_in">D</span>() &#123;<span class="comment">/*清除派生类成员的操作*/</span>&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>3.C++11中派生类能够重用直接基类定义的构造函数，相当于一种特殊的继承。类不能继承默认、拷贝和移动构造函数，如果派生类没有直接定义这些构造函数，编译器将为派生类合成它们。派生类继承基类构造函数的方式是提供一条注明了直接基类类名的using声明语句。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Dis_quote &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="keyword">using</span> Dis_quote::Dis_quote;  <span class="comment">//继承Dis_quote的构造函数，不需要再自己定义这构造函数</span></span><br><span class="line">  <span class="function"><span class="type">double</span> <span class="title">net_price</span><span class="params">(std::<span class="type">size_t</span>)</span> <span class="type">const</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>如果派生类含有自己的数据成员，继承构造函数之后，这些成员将被默认初始化。<br>和普通成员的using声明不一样，构造函数的using声明不会改变该构造函数的访问级别。且using声明语句不能指定explicit和constexpr，如果基类的构造函数是explicit或者constexpr，则继承的构造函数也拥有相同的属性。</p>
<p>4.如果派生类定义的构造函数与基类的构造函数具有相同的参数列表，则该构造函数将不会被继承，定义在派生类中的构造函数将替换继承而来的构造函数。  </p>
<h2 id="容器与继承"><a href="#容器与继承" class="headerlink" title="容器与继承"></a>容器与继承</h2><p>1.当派生类对象被赋值给基类对象时，其中的派生类部分将被“切掉”，因此容器和存在继承关系的类型无法兼容。  </p>
<p>2.可以在容器中放置基类的指针从而实现动态绑定，更好的选择是智能指针。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">vector&lt;Quote&gt; basket_q;</span><br><span class="line">basket_q.<span class="built_in">push_back</span>(<span class="built_in">Bulk_quote</span>(<span class="string">&quot;cpp&quot;</span>, <span class="number">66.6</span>, <span class="number">10</span>, <span class="number">0.8</span>));  </span><br><span class="line">cout &lt;&lt; basket_q.<span class="built_in">back</span>()-&gt;<span class="built_in">net_price</span>(<span class="number">15</span>) &lt;&lt; endl;  <span class="comment">//将按原价打印，因为Bulk_quote会被隐式转换为Quote</span></span><br><span class="line"></span><br><span class="line">vector&lt;shared_ptr&lt;Quote&gt;&gt; basket;</span><br><span class="line">basket.<span class="built_in">push_back</span>(<span class="built_in">make_shared</span>&lt;Bulk_quote&gt;(<span class="string">&quot;cpp&quot;</span>, <span class="number">66.6</span>, <span class="number">10</span>, <span class="number">0.8</span>));  </span><br><span class="line">cout &lt;&lt; basket.<span class="built_in">back</span>()-&gt;<span class="built_in">net_price</span>(<span class="number">15</span>) &lt;&lt; endl;  <span class="comment">//将可以正常调用Bulk_quote的net_price来打折扣</span></span><br></pre></td></tr></table></figure>

<p>3.C++面向对象编程的一个悖论是无法直接使用对象进行面向对象编程，而是必须使用指针和引用，由于指针会增加程序的复杂性，所以最好是定义一些辅助的类的来处理指针问题。</p>
<p>4.如果使用G++编译时出现<code>undefined reference to &quot;vtable...&quot;</code>需要检查是不是有某个类的虚函数没有提供定义，C++要求所有非纯虚函数（=0）的虚函数都必须有定义，也就是要有函数体，或者使用=default指定。  </p>
<p>5.可以采用模拟虚拷贝的方式来实现在容器中所存储指针的动态绑定。即为基类定义一个虚函数，该虚函数返回*this的拷贝，其派生类重新定义该虚函数返回自己的拷贝。然后定义一个存放该基类静态类型指针的容器，则该容器中的指针便可动态绑定到基类及其派生类的对象。eg:  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//基类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Quote</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> Quote* <span class="title">clone</span><span class="params">()</span> <span class="type">const</span> &amp; </span>&#123; <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Quote</span>(*<span class="keyword">this</span>);&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> Quote* <span class="title">clone</span><span class="params">()</span>  &amp;&amp; </span>&#123; <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Quote</span>(std::<span class="built_in">move</span>(*<span class="keyword">this</span>));&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//派生类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Bulk_quote</span> : <span class="keyword">public</span> Quote &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> Bulk_quote* <span class="title">clone</span><span class="params">()</span> <span class="type">const</span> &amp; </span>&#123; <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Bulk_quote</span>(*<span class="keyword">this</span>);&#125;</span><br><span class="line">    <span class="function"><span class="keyword">virtual</span> Bulk_quote* <span class="title">clone</span><span class="params">()</span>  &amp;&amp; </span>&#123; <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Bulk_quote</span>(std::<span class="built_in">move</span>(*<span class="keyword">this</span>));&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//存放基类指针的类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Basket</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line">    <span class="comment">//Basket使用合成的默认构造函数和拷贝控制成员</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">add_item</span><span class="params">(<span class="type">const</span> Quote &amp;sale)</span>  <span class="comment">//拷贝给定的对象</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        items.<span class="built_in">insert</span>(std::<span class="built_in">shared_ptr</span>&lt;Quote&gt;(sale.<span class="built_in">clone</span>()));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">add_item</span><span class="params">(<span class="type">const</span> Quote &amp;&amp;sale)</span>  <span class="comment">//移动给定的对象</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        items.<span class="built_in">insert</span>(std::<span class="built_in">shared_ptr</span>&lt;Quote&gt;(std::<span class="built_in">move</span>(sale).<span class="built_in">clone</span>()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">double</span> <span class="title">total_receipt</span><span class="params">(std::ostream&amp;)</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="function"><span class="type">static</span> <span class="type">bool</span> <span class="title">compare</span><span class="params">(<span class="type">const</span> std::shared_ptr&lt;Quote&gt; &amp;lhs, <span class="type">const</span> std::shared_ptr&lt;Quote&gt; &amp;rhs)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> rhs-&gt;<span class="built_in">isbn</span>() &gt; lhs-&gt;<span class="built_in">isbn</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//存放动态指针的容器</span></span><br><span class="line">    <span class="comment">//multiset用于保存多个报价，按照compare成员排序。这样就可以保存同一本书的多条交易信息</span></span><br><span class="line">    <span class="comment">//定义一个指向Quote对象的shared_ptr的multiset。这个multiset将使用一个与compare成员类型相同的</span></span><br><span class="line">    <span class="comment">//函数来对其中的元素进行排序。multiset成员的名字是items，通过类内初始值调用比较函数compare来初始化items</span></span><br><span class="line">    std::multiset&lt;std::shared_ptr&lt;Quote&gt;, <span class="keyword">decltype</span>(compare)*&gt; items&#123;compare&#125;;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">Basket::total_receipt</span><span class="params">(std::ostream&amp; os)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">   <span class="type">double</span> sum = <span class="number">0</span>;</span><br><span class="line">   <span class="comment">//iter指向ISBN相同的一批元素中的第一个，upper_bound返回一个迭代器</span></span><br><span class="line">   <span class="comment">//该迭代器指向这批元素的尾后位置</span></span><br><span class="line">   <span class="keyword">for</span> (<span class="keyword">auto</span> iter = items.<span class="built_in">cbegin</span>(); iter != items.<span class="built_in">cend</span>(); iter = items.<span class="built_in">upper_bound</span>(*iter)) &#123;</span><br><span class="line">      <span class="comment">//multiset的成员函数count返回键值相同的元素的个数 </span></span><br><span class="line">       sum += <span class="built_in">print_total</span>(os, **iter, items.<span class="built_in">count</span>(*iter));</span><br><span class="line">   &#125;</span><br><span class="line">   os &lt;&lt; <span class="string">&quot;Total Sale: &quot;</span> &lt;&lt; sum &lt;&lt; std::endl;</span><br><span class="line">   <span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="文本查询再探"><a href="#文本查询再探" class="headerlink" title="文本查询再探"></a>文本查询再探</h2><p>相关总结后补  </p>

    </div>

    
    
    
        

  <div class="followme">
    <p>Welcome to my other publishing channels</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Notes/" rel="tag"># Notes</a>
              <a href="/tags/CPP-OOP/" rel="tag"># CPP-OOP</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2015/12/23/cpp-oop-overloaded-operator/" rel="prev" title="C++学习笔记之重载运算符与类型转换">
      <i class="fa fa-chevron-left"></i> C++学习笔记之重载运算符与类型转换
    </a></div>
      <div class="post-nav-item">
    <a href="/2016/01/23/cpp-oop-template/" rel="next" title="C++学习笔记之模板与泛型编程">
      C++学习笔记之模板与泛型编程 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B4%BE%E7%94%9F%E7%B1%BB"><span class="nav-number">1.</span> <span class="nav-text">派生类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%99%9A%E5%87%BD%E6%95%B0"><span class="nav-number">2.</span> <span class="nav-text">虚函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8A%BD%E8%B1%A1%E5%9F%BA%E7%B1%BB-%E7%BA%AF%E8%99%9A%E5%87%BD%E6%95%B0"><span class="nav-number">3.</span> <span class="nav-text">抽象基类-纯虚函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6"><span class="nav-number">4.</span> <span class="nav-text">访问控制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%A7%E6%89%BF%E6%9D%83%E9%99%90"><span class="nav-number">5.</span> <span class="nav-text">继承权限</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%A7%E6%89%BF%E4%B8%AD%E7%9A%84%E7%B1%BB%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number">6.</span> <span class="nav-text">继承中的类作用域</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%A7%E6%89%BF%E4%BD%93%E7%B3%BB%E4%B8%AD%E7%9A%84%E6%8B%B7%E8%B4%9D%E6%8E%A7%E5%88%B6"><span class="nav-number">7.</span> <span class="nav-text">继承体系中的拷贝控制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%B9%E5%99%A8%E4%B8%8E%E7%BB%A7%E6%89%BF"><span class="nav-number">8.</span> <span class="nav-text">容器与继承</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%87%E6%9C%AC%E6%9F%A5%E8%AF%A2%E5%86%8D%E6%8E%A2"><span class="nav-number">9.</span> <span class="nav-text">文本查询再探</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="blueyi"
      src="/images/default_avatar.jpg">
  <p class="site-author-name" itemprop="name">blueyi</p>
  <div class="site-description" itemprop="description">心怀善意，虛怀若谷！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">104</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">68</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/blueyi" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;blueyi" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://maxwi.com/" title="http:&#x2F;&#x2F;maxwi.com" rel="noopener" target="_blank">Maxwi</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2014 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">blueyi</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="Symbols count total">750k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">11:22</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '0f8243eb2c8b2207980f',
      clientSecret: 'd159633a33519d3b7a48b0ca729032f7d1f38a41',
      repo        : 'notes',
      owner       : 'blueyi',
      admin       : ['blueyi'],
      id          : '0f854a62e1925e7ec91fc9a81fba6d85',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

    </div>
</body>
</html>
